1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
import Link from "@/components/elements/Link"
import { useRouter } from "next/router"
import { useEffect, useState } from "react"
import Header from "@/components/layouts/Header"
import apiOdoo from "@/core/utils/apiOdoo"
import { createSlug, getIdFromSlug } from "@/core/utils/slug"
import currencyFormat from "@/core/utils/currencyFormat"
import Layout from "@/components/layouts/Layout"
import { createOrUpdateItemCart } from "@/core/utils/cart"
import toast from "react-hot-toast"
import Footer from "@/components/layouts/Footer"
import Image from "@/components/elements/Image"
import LineDivider from "@/components/elements/LineDivider"
import { HeartIcon as HeartIconSolid } from "@heroicons/react/24/solid"
import { useAuth } from "@/core/utils/auth"
import { HeartIcon } from "@heroicons/react/24/outline"
import LazyLoad from "react-lazy-load"
import ProductSimilar from "@/components/products/ProductSimilar"
export async function getServerSideProps( context ) {
const { slug } = context.query
let product = await apiOdoo('GET', '/api/v1/product/' + getIdFromSlug(slug))
if (product?.length == 1) {
product = product[0]
product.description = product.description.replaceAll('<p>', '||p||')
product.description = product.description.replaceAll('</p>', '||/p||')
product.description = product.description.replace(/(<([^>]+)>)/gi, ' ')
product.description = product.description.replaceAll('||p||', '<p>')
product.description = product.description.replaceAll('||/p||', '</p>')
product.description = product.description.trim()
}
return { props: { product } }
}
export default function ProductDetail({ product }) {
const [ auth ] = useAuth()
const router = useRouter()
const { slug } = router.query
const [selectedVariant, setSelectedVariant] = useState("")
const [quantity, setQuantity] = useState("1")
const [activeVariant, setActiveVariant] = useState({
id: product.id,
code: product.code,
price: product.lowest_price,
stock: product.stock_total,
weight: product.weight,
attributes: '',
})
const [ isAddedToWishlist, setAddedToWishlist ] = useState(false)
const [ activeTab, setActiveTab ] = useState('specification')
const addOrDeleteWishlist = async () => {
if (auth) {
await apiOdoo('POST', `/api/v1/user/${auth.id}/wishlist/create-or-delete`, {
product_id: product.id
})
if (isAddedToWishlist) {
toast.success('Berhasil menghapus dari wishlist')
} else {
toast.success('Berhasil menambahkan ke wishlist')
}
setAddedToWishlist(!isAddedToWishlist)
} else {
toast.error('Login terlebih dahulu untuk melanjutkan')
router.push('/login')
}
}
useEffect(() => {
if (auth) {
const checkWishlist = async () => {
const wishlist = await apiOdoo('GET', `/api/v1/user/${auth.id}/wishlist?product_id=${product.id}`)
setAddedToWishlist(wishlist.product_total > 0 ? true : false)
}
checkWishlist()
}
}, [ auth, product ])
useEffect(() => {
if (product.variants.length == 1) {
setSelectedVariant(product.variants[0].id)
}
}, [ product ])
useEffect(() => {
if (selectedVariant != '') {
let newActiveVariant = product.variants.filter((variant) => {
return variant.id == selectedVariant
})
if (newActiveVariant.length == 1) {
newActiveVariant = newActiveVariant[0]
setActiveVariant({
id: newActiveVariant.id,
code: newActiveVariant.code,
price: newActiveVariant.price,
stock: newActiveVariant.stock,
weight: newActiveVariant.weight,
attributes: newActiveVariant.attributes.join(', '),
})
}
}
}, [selectedVariant, product])
const onchangeVariant = (e) => {
setSelectedVariant(e.target.value)
}
const onChangeQuantity = (e) => {
let inputValue = e.target.value
inputValue = parseInt(inputValue)
inputValue = Math.floor(inputValue)
setQuantity(inputValue)
}
const addItemToCart = () => {
if (product.variant_total > 1 && !selectedVariant) {
toast.error('Pilih varian terlebih dahulu untuk menambahkan ke keranjang', { duration: 2000 })
return false
}
if (quantity > 0) {
toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 })
createOrUpdateItemCart(activeVariant.id, parseInt(quantity))
} else {
toast.error('Jumlah barang yang ditambahkan minimal 1 pcs', { duration: 2000 })
}
return true
}
const checkoutProduct = () => {
if (!auth) {
toast.error('Login terlebih dahulu untuk melanjutkan', { duration: 2000 })
router.push('/login')
return
}
if (product.variant_total > 1 && !selectedVariant) {
toast.error('Pilih varian terlebih dahulu untuk melanjutkan pembelian', { duration: 2000 })
return
}
if (quantity < 0) {
toast.error('Jumlah barang yang ditambahkan minimal 1 pcs', { duration: 2000 })
return
}
router.push(`/shop/checkout?product_id=${activeVariant.id}&qty=${quantity}`)
}
const TabButton = ({ children, name }) => (
<button
type="button"
className={`font-medium pb-1 ${activeTab == name ? 'text-red_r-11 border-b border-red_r-10' : 'text-gray_r-11'}`}
onClick={() => setActiveTab(name)}
>
{ children }
</button>
)
return (
<>
<Header title={`${product.name} - Indoteknik`}/>
<Layout>
<Image
src={product.image}
alt={product.name}
className="border-b border-gray_r-6 w-full h-[300px] object-contain object-center bg-white"
/>
<div className="p-4">
<div className="flex justify-between gap-x-3">
<div>
<Link href={'/shop/brands/' + createSlug(product.manufacture.name, product.manufacture.id)}>
{product.manufacture.name ?? '-'}
</Link>
<h1 className="h2 mt-2 mb-3">{product.name}{activeVariant.attributes ? ' - ' + activeVariant.attributes : ''}</h1>
</div>
<button className="h-fit" onClick={addOrDeleteWishlist}>
{ isAddedToWishlist && (
<HeartIconSolid className="w-6 text-red_r-10" />
) }
{ !isAddedToWishlist && (
<HeartIcon className="w-6" />
) }
</button>
</div>
{product.variant_total > 1 && !selectedVariant && product.lowest_price.price > 0 ? (
<p className="text-caption-2 text-gray-800 mb-1">Harga mulai dari:</p>
) : ''}
{product.lowest_price.discount_percentage > 0 ? (
<div className="flex gap-x-1 items-center mb-1">
<p className="text-caption-2 text-gray_r-11 line-through">{currencyFormat(activeVariant.price.price)}</p>
<span className="badge-solid-red">{activeVariant.price.discount_percentage}%</span>
</div>
) : ''}
{product.lowest_price.price > 0 ? (
<p className="text-body-lg font-semibold">{currencyFormat(activeVariant.price.price_discount)}</p>
) : (
<p className="text-gray_r-11">Dapatkan harga terbaik, <a href="">hubungi kami.</a></p>
)}
</div>
<LineDivider />
<div className="p-4">
<div className="">
<label className="form-label mb-2">Pilih: <span className="text-gray_r-11 font-normal">{product.variant_total} Varian</span></label>
<select name="variant" className="form-input" value={selectedVariant} onChange={onchangeVariant} >
<option value="" disabled={selectedVariant != "" ? true : false}>Pilih Varian...</option>
{product.variants.length > 1 ? (
product.variants.map((variant) => {
return (
<option key={variant.id} value={variant.id}>{variant.attributes.join(', ')}</option>
)
})
) : (
<option key={product.variants[0].id} value={product.variants[0].id}>{product.variants[0].name}</option>
)}
</select>
</div>
<label htmlFor="quantity" className="form-label mb-1 mt-3">Jumlah</label>
<div className="flex gap-x-2 mt-2">
<input type="number" name="quantity" id="quantity" className="form-input h-full w-5/12 text-center" value={quantity} onChange={onChangeQuantity} />
<button
className="btn-yellow w-full"
onClick={addItemToCart}
disabled={(product.lowest_price.price == 0 ? true : false)}
>
Keranjang
</button>
<button
onClick={checkoutProduct}
className="btn-solid-red w-full"
>
Beli
</button>
</div>
</div>
<LineDivider />
<div className="p-4">
<h2 className="font-bold mb-4">Informasi Produk</h2>
<div className="flex gap-x-3 mb-4">
<TabButton name="specification">Spesifikasi</TabButton>
<TabButton name="description">Deskripsi</TabButton>
<TabButton name="information">Info Penting</TabButton>
</div>
<div className={`border border-gray_r-6 rounded divide-y ${activeTab == 'specification' ? 'block' : 'hidden'}`}>
<ProductSpecification label="Jumlah Varian">
<p className="text-gray-800">{product.variant_total} Varian</p>
</ProductSpecification>
<ProductSpecification label="Nomor SKU">
<p className="text-gray-800" id="sku_number">SKU-{activeVariant.id}</p>
</ProductSpecification>
<ProductSpecification label="Part Number">
<p className="text-gray-800" id="part_number">{activeVariant.code}</p>
</ProductSpecification>
<ProductSpecification label="Stok">
<div className="flex gap-x-2" id="stock">
{activeVariant.stock > 0 ? (activeVariant.stock > 5 && (
<>
<div className="badge-solid-red">Ready Stock</div>
<div className="badge-gray">{activeVariant.stock > 5 ? '> 5' : '< 5'}</div>
</>
)) : '0'}
</div>
</ProductSpecification>
<ProductSpecification label="Part Number">
<p className="text-gray-800" id="weight">{activeVariant.weight > 0 ? activeVariant.weight : '1'} KG</p>
</ProductSpecification>
</div>
<div
className={`text-gray-800 leading-7 ${activeTab == 'description' ? 'block' : 'hidden'}`}
dangerouslySetInnerHTML={{__html: (product.description != '' ? product.description : 'Belum ada deskripsi produk.')}}
></div>
</div>
<LineDivider />
<LazyLoad>
<ProductSimilar productId={getIdFromSlug(slug || '')} />
</LazyLoad>
<Footer />
</Layout>
</>
)
}
const ProductSpecification = ({ children, ...props }) => {
return (
<div className="flex p-3 justify-between items-center gap-x-1">
<h3 className="text-gray-900">{ props.label }</h3>
{ children }
</div>
)
}
|